const app = getApp()
const EARTH_RADIUS = 6378.137; //地球半径
const profile = require("../../utils/profile.js")

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    radius: 2,
    markers: [],
    circles: [],
    start_at: '',
    end_at: '',
    refreshMapFlag: true,
    mpCtx: null,
    mpUpdateGetBuildingsSwitch: true,
    showBuildings: [],
    choosedBuildings: [],
    toChoosePointSumCount: 0,
    price: 0,
    sumPrice: 0,
    chooseCountLimit: 10,
    cuForm: {
      start_date: '',
      end_date: '',
      sale_id: null,
      customer_id: null,
      orderList: []
    },
    customerList: [],
    customer_id: null,
    index : -1
  },

  onReady() {   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    const that = this
    let datas=JSON.parse(option.data)
    //  console.log(13123, option, option.start_at, option.end_at)
    // console.log("options",option)
    that.setData({
      choosedBuildings: datas,
      start_at: option.start_at,
      end_at: option.end_at
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCustomerList()
  },

  changePoint(e) {
    const that = this
    that.changeBuildingChecked(e.currentTarget.dataset) 
  },

  changeBuildingChecked(e){
   let choosedBuildings = this.data.choosedBuildings
    for (var i in choosedBuildings){
      if(choosedBuildings[i].id == e.building){
        choosedBuildings[i].pointTypeCountVOList[e.idx].checked = !choosedBuildings[i].pointTypeCountVOList[e.idx].checked
      }
    }
    this.setData({
      choosedBuildings: choosedBuildings
    })
  },

  changePointShow(e) {
    const that = this
    let buildings = JSON.parse(JSON.stringify(that.data.choosedBuildings)) 
    buildings[parseInt(e.currentTarget.dataset.idx)].show = !buildings[parseInt(e.currentTarget.dataset.idx)].show;
    that.setData({
      choosedBuildings: buildings
    })
    // console.log(that.data.choosedBuildings)
  },

  submitCount(e){
    let orderList = []
    const form = e.detail.value
    if(!this.data.customer_id){
      this.showToast("请先选择客户")
      return false
    }
    if(JSON.stringify(form) == "{}"){
      this.showToast("请先选择要推送的点位")
      return false
    }
    for(const key in form){ 
      let arr = key.split('_')
      if(form[key] == null){
        this.showToast("请先选择要推送的点位")
        return false
      }
      if(parseInt(form[key]) > parseInt(arr[3])){
        this.showToast("存在输入大于可用点位的数量")
        return false
      }
      if(form[key] == ''){
        this.showToast("请输入需要选择的数量")
        return false
      }
      let order = {
        building_area_id: parseInt(arr[0]),
        media_type_id: parseInt(arr[1]),
        need_number: parseInt(form[key])
      }
      orderList.push(order)
      // console.log(orderList)
    }
    let cuForm = {
      beginDate: null,
      endDate: null,
      sale_id: null,
      customer_id: null,
      orderList: []
    }
    cuForm.customer_id = this.data.customer_id
    cuForm.beginDate = this.data.start_at
    cuForm.endDate = this.data.end_at
    cuForm.orderList = orderList
  
    this.dopost(cuForm)
  },
  bindChange: function(e) {
    // console.log(e)
},
dopost(upData) { 
  const that = this
  wx.request({
    url: profile.domain + '/api/mini/sale_intention_order/save',
    header: {
      "content-type": "application/json;charset=UTF-8",
      "Authorization": wx.getStorageSync('token')
    },
    data: upData,
    method: 'POST',
    success: function (res) {
      that.showToast("推送成功")
      wx.navigateBack({
        delta: 1
      })
    },
    fail: function () {
      that.showToast("推送失败")
      // doFail();
    },
  })
},
showToast(title) {
  wx.showToast({
    title: title,
    icon: 'none',
    duration: 2000
  })
},
getCustomerList(){
    const that = this
    wx.request({
      url: profile.domain + '/api/mini/sale_intention_order/listCustomerOfSale',
      header: {
        "content-type": "application/json;charset=UTF-8",
        "Authorization": wx.getStorageSync('token')
      },
      method: 'GET',
      success: function (res) {
        // that.showToast("推送成功")
        const list = res.data.data
        if(list != null && list.length > 0){
        that.setData({
          customerList: list
        })
      }else {
        that.showToast("暂未发现您绑定了客户")
      }
      },
      fail: function () {
        // that.showToast("推送失败")
        // doFail();
      },
    })
},
PickerChangeType(e) {
  this.setData({
    index: e.detail.value,
    customer_id: this.data.customerList[e.detail.value].id
  })
},
})